﻿using System.Windows.Controls;
using System.Windows.Input;
using Oxage.Models;

namespace Oxage.Views
{
	public partial class SimulatorView : UserControl
	{
		#region Constructor
		public SimulatorView()
		{
			InitializeComponent();
			ViewModel.Driver = sim;

			this.Focusable = true;
			this.KeyDown += new KeyEventHandler(Simulator_KeyDown);
		}
		#endregion

		#region Public properties
		public SimulatorViewModel ViewModel
		{
			get
			{
				return this.DataContext as SimulatorViewModel;
			}
		}
		#endregion

		#region Event handlers
		private void Simulator_KeyDown(object sender, KeyEventArgs e)
		{
			var sim = App.ViewModel.View.Simulator.ViewModel;

			switch (e.Key)
			{
				case Key.Left:
					sim.Enable(SimulatorViewModel.AXIS_X, true);
					sim.Dir(SimulatorViewModel.AXIS_X, false);
					sim.Step(SimulatorViewModel.AXIS_X);
					sim.Enable(SimulatorViewModel.AXIS_X, false);
					break;

				case Key.Right:
					sim.Enable(SimulatorViewModel.AXIS_X, true);
					sim.Dir(SimulatorViewModel.AXIS_X, true);
					sim.Step(SimulatorViewModel.AXIS_X);
					sim.Enable(SimulatorViewModel.AXIS_X, false);
					break;

				case Key.Up:
					sim.Enable(SimulatorViewModel.AXIS_Y, true);
					sim.Dir(SimulatorViewModel.AXIS_Y, false);
					sim.Step(SimulatorViewModel.AXIS_Y);
					sim.Enable(SimulatorViewModel.AXIS_Y, false);
					break;

				case Key.Down:
					sim.Enable(SimulatorViewModel.AXIS_Y, true);
					sim.Dir(SimulatorViewModel.AXIS_Y, true);
					sim.Step(SimulatorViewModel.AXIS_Y);
					sim.Enable(SimulatorViewModel.AXIS_Y, false);
					break;
			}
		}
		#endregion
	}
}
